{% extends 'base.html' %}
{% load static %}

{% block title %}我的借用申请{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-user me-2"></i>我的借用申请</h5>
                    <a href="{% url 'assets:borrow_request_create' %}" class="btn btn-primary">
                        <i class="fas fa-plus me-1"></i>新建申请
                    </a>
                </div>
                
                <div class="card-body">
                    <!-- 筛选 -->
                    <form method="get" class="row g-3 mb-4">
                        <div class="col-md-4">
                            <select class="form-select" name="status">
                                <option value="">全部状态</option>
                                {% for value, label in status_choices %}
                                    <option value="{{ value }}" {% if value == selected_status %}selected{% endif %}>
                                        {{ label }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-8">
                            <button type="submit" class="btn btn-outline-primary">
                                <i class="fas fa-filter me-1"></i>筛选
                            </button>
                        </div>
                    </form>

                    <!-- 申请列表 -->
                    {% if page_obj %}
                        <div class="row">
                            {% for borrow in page_obj %}
                                <div class="col-md-6 mb-4">
                                    <div class="card h-100 {% if borrow.is_overdue %}border-warning{% endif %}">
                                        <div class="card-header d-flex justify-content-between align-items-center">
                                            <h6 class="mb-0">{{ borrow.asset.name }}</h6>
                                            {% if borrow.status == 'pending' %}
                                                <span class="badge bg-secondary">{{ borrow.get_status_display }}</span>
                                            {% elif borrow.status == 'approved' %}
                                                <span class="badge bg-success">{{ borrow.get_status_display }}</span>
                                            {% elif borrow.status == 'borrowed' %}
                                                <span class="badge bg-primary">{{ borrow.get_status_display }}</span>
                                            {% elif borrow.status == 'returned' %}
                                                <span class="badge bg-info">{{ borrow.get_status_display }}</span>
                                            {% elif borrow.status == 'overdue' %}
                                                <span class="badge bg-warning">{{ borrow.get_status_display }}</span>
                                            {% elif borrow.status == 'rejected' %}
                                                <span class="badge bg-danger">{{ borrow.get_status_display }}</span>
                                            {% else %}
                                                <span class="badge bg-light text-dark">{{ borrow.get_status_display }}</span>
                                            {% endif %}
                                        </div>
                                        <div class="card-body">
                                            <p class="card-text">
                                                <small class="text-muted">{{ borrow.asset.asset_number }} | {{ borrow.asset.category.name }}</small>
                                            </p>
                                            
                                            <div class="mb-2">
                                                <strong>借用目的：</strong>
                                                <span class="badge bg-info">{{ borrow.get_purpose_display }}</span>
                                            </div>
                                            
                                            <div class="mb-2">
                                                <small class="text-muted">{{ borrow.purpose_description|truncatechars:50 }}</small>
                                            </div>
                                            
                                            <div class="row text-center mt-3">
                                                <div class="col-4">
                                                    <small class="text-muted">申请时间</small><br>
                                                    <small>{{ borrow.created_at|date:"m-d H:i" }}</small>
                                                </div>
                                                <div class="col-4">
                                                    <small class="text-muted">预计归还</small><br>
                                                    <small>{{ borrow.expected_return_date|date:"m-d H:i" }}</small>
                                                </div>
                                                <div class="col-4">
                                                    {% if borrow.borrowed_at %}
                                                        <small class="text-muted">借出时间</small><br>
                                                        <small>{{ borrow.borrowed_at|date:"m-d H:i" }}</small>
                                                    {% elif borrow.approved_at %}
                                                        <small class="text-muted">审批时间</small><br>
                                                        <small>{{ borrow.approved_at|date:"m-d H:i" }}</small>
                                                    {% else %}
                                                        <small class="text-muted">等待审批</small><br>
                                                        <small>-</small>
                                                    {% endif %}
                                                </div>
                                            </div>
                                            
                                            {% if borrow.is_overdue %}
                                                <div class="alert alert-warning mt-3 mb-0">
                                                    <small><i class="fas fa-exclamation-triangle me-1"></i>已逾期 {{ borrow.days_overdue }} 天</small>
                                                </div>
                                            {% endif %}
                                            
                                            {% if borrow.approver and borrow.status == 'rejected' %}
                                                <div class="alert alert-danger mt-3 mb-0">
                                                    <small><strong>拒绝原因：</strong>{{ borrow.approval_notes|default:"无" }}</small>
                                                </div>
                                            {% endif %}
                                        </div>
                                        <div class="card-footer">
                                            <div class="d-flex justify-content-between align-items-center">
                                                <small class="text-muted">
                                                    {% if borrow.approver %}
                                                        审批人：{{ borrow.approver.get_full_name|default:borrow.approver.username }}
                                                    {% else %}
                                                        等待审批
                                                    {% endif %}
                                                </small>
                                                <a href="{% url 'assets:borrow_request_detail' borrow.pk %}" class="btn btn-outline-primary btn-sm">
                                                    <i class="fas fa-eye me-1"></i>查看详情
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>

                        <!-- 分页 -->
                        {% if page_obj.has_other_pages %}
                            <nav aria-label="我的借用申请分页">
                                <ul class="pagination justify-content-center">
                                    {% if page_obj.has_previous %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page=1{% if selected_status %}&status={{ selected_status }}{% endif %}">首页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if selected_status %}&status={{ selected_status }}{% endif %}">上一页</a>
                                        </li>
                                    {% endif %}

                                    <li class="page-item active">
                                        <span class="page-link">第 {{ page_obj.number }} 页，共 {{ page_obj.paginator.num_pages }} 页</span>
                                    </li>

                                    {% if page_obj.has_next %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if selected_status %}&status={{ selected_status }}{% endif %}">下一页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if selected_status %}&status={{ selected_status }}{% endif %}">末页</a>
                                        </li>
                                    {% endif %}
                                </ul>
                            </nav>
                        {% endif %}
                    {% else %}
                        <div class="text-center py-5">
                            <i class="fas fa-clipboard-list fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">暂无借用申请</h5>
                            <p class="text-muted">点击上方按钮创建第一个借用申请</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
